﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Attacker
{
    class Database_mag
    {
        private string connString;
        private SqlConnection conn;
        private SqlCommand cmd;
        private SqlDataAdapter sda;
        private DataTable dt;
		public int tag;

		public Database_mag()
		{
			tag = 0;
		}

		public void Connection()
        {
            connString = "Data Source=.;Initial Catalog=Watermarking;Integrated Security=True";
            conn = new SqlConnection(connString);
            conn.Open();
			tag = 1;
            MessageBox.Show("数据库连接成功");
            cmd = conn.CreateCommand();
        }
        public SqlDataReader Select(string s)
        {
            cmd.CommandText = s;
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        public void Update(string s)
        {
            cmd.CommandText = s;
            cmd.ExecuteNonQuery();
        }
        public void Backups()
        {
            cmd.CommandText = "delete from backups2 where 1=1 insert into backups2 select* from weather";//测试用表
            //cmd.CommandText = "insert into backups select * from weather";//测试用表
            cmd.ExecuteNonQuery();
        }
        public void Revert()
        {
			cmd.CommandText = "delete from weather where 1=1 insert into weather select* from backups2;";
			cmd.ExecuteNonQuery();

		}
        public DataTable Read_table()
        {
            cmd.CommandText = "select * from weather";//测试用表
            sda = new SqlDataAdapter(cmd);
            dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }
        public void Update_table()
        {
            new SqlCommandBuilder(sda);
            sda.Update(dt.GetChanges());
            dt.AcceptChanges();
            sda = null;
            dt = null;
        }
    }
}
